#include "searchhistorydialog.h"
#include "ui_searchhistorydialog.h"

SearchHistoryDialog::SearchHistoryDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SearchHistoryDialog)
{
    ui->setupUi(this);

    // Create model
    locationModel = new QStringListModel(this);
    ui->locationListView->setModel(locationModel);

    restaurantsModel = new QStringListModel(this);
    ui->RestaurantDetailListView->setModel(restaurantsModel);

    connect(ui->locationListView, SIGNAL(clicked(QModelIndex)), this, SLOT(loadFoundRestaurants(QModelIndex)));
}

SearchHistoryDialog::~SearchHistoryDialog()
{
    delete ui;
}

void SearchHistoryDialog::loadData(QList<SearchResult*> searchResults)
{
    //Update the search result
    m_searchResults = searchResults;

    foreach(SearchResult* item, searchResults)
    {
       locationModel->insertRow(locationModel->rowCount());
       QModelIndex index = locationModel->index(locationModel->rowCount()-1);
       locationModel->setData(index, item->getLocation());
    }
}

void SearchHistoryDialog::loadFoundRestaurants(const QModelIndex &index)
{
    //Clear the current restaurant list;
    restaurantsModel->setStringList(QStringList());

    foreach(SearchResult* item, m_searchResults)
    {
        if(item && index.data().toString() == item->getLocation() )
        {
           restaurantsModel->setStringList(item->getResults());
           break;
        }
    }
}
